﻿using System;
using Topics.Radical.Validation;

namespace Dexter.Domain.Model {
	public class PluginData : EntityBase {
		/// <summary>
		/// 	Initializes a new instance of the <see cref = "EntityBase" /> class.
		/// </summary>
		protected PluginData() {}

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "EntityBase" /> class.
		/// </summary>
		protected PluginData(Plugin parent, string key, string data) {
			Parent = parent;
			Key = key;
			Data = data;
		}

		public virtual Plugin Parent { get; set; }
		public virtual string Key { get; set; }
		public virtual string Data { get; set; }

		public static PluginData CreateNewPluginData(Plugin parent, string key, string data) {
			if(parent == null)
				throw new ArgumentNullException("parent");

			if(key == null)
				throw new ArgumentNullException("key");

			if (data == null)
				throw new ArgumentNullException("data");

			if(key == string.Empty)
				throw new ArgumentException("key");

			return new PluginData(parent, key, data);
		}

		public virtual void ValidateForSave() {
			if (Id != 0)
				throw new ArgumentException("Invalid data id", "Id");

			if (Key.IsNullOrEmpty())
				throw new ArgumentException("Invalid data key", "Key");

			if (Data.IsNullOrEmpty())
				throw new ArgumentException("Invalid data data", "Data");

			if (Parent == null)
				throw new ArgumentException("Invalid data parent", "Parent");
		}

		public virtual void ValidateForUpdate()
		{
			if(Id < 1)
				throw new ArgumentException("Invalid data id", "Id");

			if (Key.IsNullOrEmpty())
				throw new ArgumentException("Invalid data key", "Key");

			if (Data.IsNullOrEmpty())
				throw new ArgumentException("Invalid data data", "Data");

			if (Parent == null)
				throw new ArgumentException("Invalid data parent", "Parent");
		}
	}
}